//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public class ScatterChart : ChartBase, IDataLabelsOwner
    {
        private ValueAxis _firstAxis = NewAxis(AxisPosition.Left);
        static ValueAxis NewAxis (AxisPosition p)
        {
            return new ValueAxis{Position = p}
            ;
        }

        private ValueAxis _secondAxis = NewAxis(AxisPosition.Bottom);
        private ObservableCollection<ScatterSeries> _series;
        private DataLabels dlbls;
        private ScatterStyle scatterStyle = ScatterStyle.LineMarker;

        private bool varyColors;

        public ScatterStyle ScatterStyle
        {
            get { return scatterStyle; }
            set
            {
                scatterStyle = value;
                notifyPropertyChanged("ScatterStyle");
            }
        }

        public bool VaryColors
        {
            get { return varyColors; }
            set
            {
                varyColors = value;
                notifyPropertyChanged("VaryColors");
            }
        }

        public ObservableCollection<ScatterSeries> Series
        {
            get
            {
                if (_series == null)
                    _series = CreateSeriesCollection<ScatterSeries>();
                return _series;
            }
        }


        internal ValueAxis FirstAxis
        {
            get
            {
                return _firstAxis;
            }
            set
            {
                _firstAxis = value;
            }
        }

        internal ValueAxis SecondAxis
        {
            get { return _secondAxis; }
            set
            {
                _secondAxis = value;
            }
        }

        public ValueAxis HorizontalAxis
        {
            get
            {
                switch (FirstAxis.Position)
                {
                    case AxisPosition.Bottom:
                    case AxisPosition.Top:
                        return FirstAxis;
                }
                return SecondAxis;
            }
        }

        public ValueAxis VerticalAxis
        {
            get
            {
                switch (FirstAxis.Position)
                {
                    case AxisPosition.Left:
                    case AxisPosition.Right:
                        return FirstAxis;
                }
                return SecondAxis;
            }
        }


        public DataLabels DataLabels
        {
            get { return dlbls; }
            set { dlbls = value; notifyPropertyChanged("DataLabels");}
        }

        #region IChartWithDisplayInformation Members

        #endregion

        protected override void VerifyAxes()
        {
            VerticalAxis.CrossAxis = HorizontalAxis;
            HorizontalAxis.CrossAxis = VerticalAxis;
        }

        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("scatterChart"))
            {
                ScatterStyle =
                    c.ReadOptionalElementEnumAttribute("scatterStyle", "val", ScatterStyle.Marker,
                                                                     ScatterStyle.Marker);
                VaryColors = ChartHelper.ReadVaryColors(c);
                Series.Clear();
                while (c.ReaderLocalName == "ser")
                {
                    var s = new ScatterSeries();
                    s.ReadXmlCore(c);
                    Series.Add(s);
                }
                DataLabels = c.ReadOptionalElement<DataLabels>(DataLabels.xmlName);
                AxisResolver r = ReadContextData.GetAxisResolver(c);
                r.AddAction(c.ReadElementUIntegerAttribute("axId"), delegate(Axis a) { FirstAxis = (ValueAxis) a; });
                r.AddAction(c.ReadElementUIntegerAttribute("axId"), delegate(Axis a) { SecondAxis = (ValueAxis) a; });
                c.WarnIf("ext");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("scatterChart"))
            {
                c.WriteOptionalElementEnumAttribute("scatterStyle", "val", ScatterStyle,
                                                                  ScatterStyle.Marker, ScatterStyle.Marker);
                ChartHelper.WriteVaryColors(c, VaryColors);
                foreach (ScatterSeries s in Series)
                    c.WriteElement(s);
                c.WriteOptionalElement(DataLabels);
                c.WriteElementUIntegerAttribute("axId", FirstAxis.Id);
                c.WriteElementUIntegerAttribute("axId", SecondAxis.Id);
                c.WarnIf("ext");
            }
        }

        internal override IEnumerable<Axis> GetAxes()
        {
            yield return FirstAxis;
            yield return SecondAxis;
        }
    }
}